package com.homegoods.home.location;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.kedll.library.location.LocationInterface;
import com.kedll.library.location.entity.LocationEntity;
import com.kedll.library.location.listener.OnLocationChangeListener;

/**
 *
 * @author 华哥
 * @date 2016/9/22
 * 高德定位
 */
public class AMapLocationImpl implements AMapLocationListener, LocationInterface {

    /**
     * 声明mLocationOption对象
     */
    public AMapLocationClientOption mLocationOption = null;
    /**
     * 高德地图定位相关
     */
    private AMapLocationClient mLocationManagerProxy;
    /**
     * 定位成功位置信息回调监听
     */
    private OnLocationChangeListener listener;

    private Context context;

    /**
     * 定位信息
     */
    private LocationEntity entity = new LocationEntity();

    public AMapLocationImpl(Context context, OnLocationChangeListener listener) {
        this.context = context;
        this.listener = listener;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //定位成功回调信息，设置相关消息
//                aMapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
//                aMapLocation.getLatitude();//获取纬度
//                aMapLocation.getLongitude();//获取经度
//                aMapLocation.getAccuracy();//获取精度信息
//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                Date date = new Date(aMapLocation.getTime());
//                df.format(date);//定位时间
//                aMapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
//                aMapLocation.getCountry();//国家信息
//                aMapLocation.getProvince();//省信息
//                aMapLocation.getCity();//城市信息

//                aMapLocation.getDistrict();//城区信息
//                aMapLocation.getStreet();//街道信息
//                aMapLocation.getStreetNum();//街道门牌号信息
//                aMapLocation.getCityCode();//城市编码
//                aMapLocation.getAdCode();//地区编码
//                aMapLocation.getAOIName();//获取当前定位点的AOI信息
//                aMapLocation.getLatitude(); // 纬度
//                aMapLocation.getLongitude(); // 经度

                entity.setCity(aMapLocation.getCity());
                entity.setLan(aMapLocation.getLatitude());
                entity.setLng(aMapLocation.getLongitude());
                Log.i("location", entity.toString());
                if (listener != null) {
                    listener.onLocationChangeListener(entity);
                }
                return;
            }
            //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
            Log.e("AmapError", "location Error, ErrCode:"
                    + aMapLocation.getErrorCode() + ", errInfo:"
                    + aMapLocation.getErrorInfo());
        }
    }

    /**
     * 启动定位
     */
    @Override
    public void startLocation(long interval) {
        if (mLocationManagerProxy == null) {
            mLocationManagerProxy = new AMapLocationClient(context);
            mLocationManagerProxy.setLocationListener(this);
        }
        //初始化定位参数
        if (mLocationOption == null) {
            mLocationOption = new AMapLocationClientOption();
            //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
            //设置是否返回地址信息（默认返回地址信息）
            mLocationOption.setNeedAddress(true);
            //设置是否强制刷新WIFI，默认为强制刷新
            mLocationOption.setWifiScan(false);
            //设置是否允许模拟位置,默认为false，不允许模拟位置
            mLocationOption.setMockEnable(false);
            if (interval == 0) {
                mLocationOption.setOnceLocation(true);
            } else {
                //设置定位间隔,单位毫秒,默认为2000ms
                mLocationOption.setInterval(interval);
            }
        }
        //给定位客户端对象设置定位参数
        mLocationManagerProxy.setLocationOption(mLocationOption);
        //启动定位
        mLocationManagerProxy.startLocation();
    }

    /**
     * 停止定位，并销毁定位资源
     */
    @Override
    public void stopLocation() {
        if (mLocationManagerProxy != null) {
            mLocationManagerProxy.stopLocation();//停止定位
            mLocationManagerProxy.onDestroy();
        }
        mLocationManagerProxy = null;
    }
}
